using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;

/// <summary>
/// Summary description for TaiKhoanDAO
/// </summary>
public class TaiKhoanDAO:DataProvider
{
	public TaiKhoanDAO()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public ArrayList LayMang_TaiKhoan()
    {
        connect();
        ArrayList arr = new ArrayList();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Select_TaiKhoan", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                TaiKhoanDTO obj = new TaiKhoanDTO();
                obj.MaTaiKhoan = dr.GetString(0);
                obj.PassWord = dr.GetString(1);
                obj.LoaiTaiKhoan = dr.GetInt32(2);
                arr.Add(obj);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            disconnect();
        }
        return arr;

    }
    public TaiKhoanDTO getTaiKhoan(String MaTaiKhoan, String Password)
    {
        
        TaiKhoanDTO obj = new TaiKhoanDTO();
        try
        {
            connect();
            SqlCommand cmd = new SqlCommand("sp_KiemTra_TaiKhoan", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@MaTaiKhoan", SqlDbType.NVarChar);
            cmd.Parameters["@MaTaiKhoan"].Value = MaTaiKhoan.ToString();
            cmd.Parameters["@MaTaiKhoan"].Direction = ParameterDirection.Input;

            cmd.Parameters.Add("@PassWord", SqlDbType.NVarChar);
            cmd.Parameters["@PassWord"].Value = Password.ToString();
            cmd.Parameters["@PassWord"].Direction = ParameterDirection.Input;

            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

                obj.MaTaiKhoan = dr.GetString(0);
                obj.PassWord = dr.GetString(1);
                obj.LoaiTaiKhoan = dr.GetInt32(2);
               
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            disconnect();
        }
        return obj;

    }
    public void insert(TaiKhoanDTO obj)
    {
        connect();

        try
        {
            SqlCommand cmd = new SqlCommand("sp_Insert_TaiKhoan", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@MaTaiKhoan", SqlDbType.NVarChar);
            cmd.Parameters["@MaTaiKhoan"].Value = obj.MaTaiKhoan.ToString();
            cmd.Parameters["@MaTaiKhoan"].Direction = ParameterDirection.Input;

            cmd.Parameters.Add("@PassWord", SqlDbType.NVarChar);
            cmd.Parameters["@PassWord"].Value = obj.PassWord.ToString();
            cmd.Parameters["@PassWord"].Direction = ParameterDirection.Input;

            cmd.Parameters.Add("@LoaiTaiKhoan", SqlDbType.Int);
            cmd.Parameters["@LoaiTaiKhoan"].Value = obj.LoaiTaiKhoan;
            cmd.Parameters["@LoaiTaiKhoan"].Direction = ParameterDirection.Input;

            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            disconnect();
        }
    }
    public void delete(string MaTaiKhoan)
    {
        connect();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Delete_TaiKhoan", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@MaTaiKhoan", SqlDbType.VarChar);
            cmd.Parameters["@MaTaiKhoan"].Value = MaTaiKhoan;
            cmd.Parameters["@MaTaiKhoan"].Direction = ParameterDirection.Input;
            cmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            disconnect();
        }
    }
    public void update(TaiKhoanDTO obj)
    {
        connect();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Update_TaiKhoan", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@MaTaiKhoan", SqlDbType.VarChar);
            cmd.Parameters["@MaTaiKhoan"].Value = obj.MaTaiKhoan.ToString();
            cmd.Parameters["@MaTaiKhoan"].Direction = ParameterDirection.Input;

            cmd.Parameters.Add("@PassWord", SqlDbType.NVarChar);
            cmd.Parameters["@PassWord"].Value = obj.PassWord.ToString();
            cmd.Parameters["@PassWord"].Direction = ParameterDirection.Input;

            cmd.Parameters.Add("@LoaiTaiKhoan", SqlDbType.Int);
            cmd.Parameters["@LoaiTaiKhoan"].Value = obj.LoaiTaiKhoan;
            cmd.Parameters["@LoaiTaiKhoan"].Direction = ParameterDirection.Input;

            cmd.ExecuteNonQuery();

        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            disconnect();
        }
    }
}
